<template>
  <div>
    <el-dialog
      v-model="createVisible"
      :title="operation == 'create' ? '添加分组' : '编辑分组'"
      @before-ok="createOk"
      @close="close(createRef)"
      width="400px"
    >
      <el-form
        :model="createForm"
        ref="createRef"
        :rules="createRules"
        label-width="0"
        size="large"
      >
        <el-form-item prop="group_name" class="group-form-item ">
          <el-input
            v-model="createForm.group_name"
            placeholder="请输入分组名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="flex center">
          <el-button @click="createVisible = false">取消</el-button>
          <el-button type="primary" :loading="btnLoading" :disabled="btnLoading" @click="createOk(createRef)"
            >确定</el-button
          >
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts">
export default {
  name: "mateFilesGroupCreate",
};
</script>
<script lang="ts" setup>
import { ref, h, reactive, getCurrentInstance, nextTick } from "vue";
import type { FormInstance } from "element-plus";
import type { Result , ResultError } from "@/types";
import { useConfig } from "@/store";
import {
    createGroup,
    updateGroup,
} from "@/api/mate/files";
const {
  proxy: { $message, $utils },
} = getCurrentInstance() as any;

const configStore = useConfig();

const emit = defineEmits(["success"]);

const operation = ref("create");

const operationId = ref(0);
const createVisible = ref(false);
const createRef: any = ref<typeof FormInstance>();
const createForm = ref({
  group_name: "",
});
const createRules: any = reactive({
  config_name: [
    {
      required: true,
      message: "分组名称不能为空",
      trigger: "blur",
    },
  ],
});

const open = (id = 0,group_name = '') => {
  if (id != 0) {
    operation.value = "update";
    operationId.value = id;
    createForm.value.group_name = group_name
  } else {
    operation.value = "create";
  }
  nextTick(() => {
    toInit();
  });
  createVisible.value = true;
};

const toInit = () => {
  if (!operationId.value) {
    return;
  }
};

const btnLoading = ref(false)

const createOk = async (formEl: typeof FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid:any, fields: any) => {
    if (valid) {
      let operationApi: any = null;
      if (operation.value == "create") {
        operationApi = createGroup(createForm.value);
      } else {
        operationApi = updateGroup(
          Object.assign(
            {
              id: operationId.value,
            },
            createForm.value
          )
        );
      }
      if(btnLoading.value){
        return
      }
      btnLoading.value = true
      operationApi
        .then((res: Result) => {
          $message.success(res.message);
          close(formEl);
          emit("success");
          btnLoading.value = false
        })
        .catch((err: ResultError) => {
          $utils.errorMsg(err);
          btnLoading.value = false
        });
    }
  });
};

const createLoading: any = ref(false);

const close = async (formEl: typeof FormInstance | undefined) => {
  if (formEl) {
    formEl.resetFields();
  }
  createForm.value.group_name = '';
  operationId.value = 0;
  createVisible.value = false;
};

defineExpose({ open });
</script>
<style scoped>
.group-form-item {
  margin-bottom: 0 !important;
}
</style>